<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Sexoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('sexoption');
	}
	public function ajax_sexoption_get(){
		$sexoptionEntity =  new CI_Sex_option_entity();
		$sexoption = $sexoptionEntity->getSexOptionByCode($_REQUEST['code']);
		echo json_encode($sexoption);
	}
	public function ajax_sexoption_list(){
		$sexoptionEntity =  new CI_Sex_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$sexoptionList = $sexoptionEntity->getSexOptionListByLanguage($defaultLanguageId);
		$data['sexoptionList'] = $sexoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/sexoptions/index', $data);
	}
	public function ajax_sexoption_save(){
		$sexoptionNameList = $_REQUEST['sexoption'];
		$sexoptionNameDefault = '';
		foreach ($sexoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$sexoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($sexoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_sexoption_require');
		}else{
			$code = 0;
			foreach ($sexoptionNameList as $key => $value) {
				$sexoptionEntity =  new CI_Sex_option_entity();
				if($value != null && $value != ''){
					$sexoptionEntity->setSex($value);
				}else{
					$sexoptionEntity->setSex($sexoptionNameDefault);
				}
				$sexoptionEntity->setLanguageId($key);
				$sexoptionEntity->setCode($code);
				$id = $sexoptionEntity->save();
				if($code == 0){
					$code = $id;
					$sexoptionEntity->setId($id);
					$sexoptionEntity->setCode($code);
					$sexoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_sexoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_sexoption_update(){
		$sexoptionNameList = $_REQUEST['sexoption'];
		$sexoptionIdList = $_REQUEST['sexoption_id'];
		$sexoptionCode = $_REQUEST['sexoption_code'];
		$sexoptionNameDefault = '';
		foreach ($sexoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$sexoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($sexoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_sexoption_require');
		}else{
			foreach ($sexoptionNameList as $key => $value) {
				$sexoptionEntity =  new CI_Sex_option_entity();
				if($value != null && $value != ''){
					$sexoptionEntity->setSex($value);
				}else{
					$sexoptionEntity->setSex($sexoptionNameDefault);
				}
				$sexoptionEntity->setLanguageId($key);
				$sexoptionEntity->setCode($sexoptionCode);
				$sexoptionEntity->setId($sexoptionIdList[$key]);
				$sexoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_sexoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_sexoption_delete(){
		$result = array();
		$sexoptionEntity =  new CI_Sex_option_entity();
		$result['result'] = $sexoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_sexoption_delete_success');
		}else{
			$result['message'] = lang('escort_sexoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>